package JDBC_2.DAO2;

import JDBC_1.bean.Customer;

import java.sql.Connection;
import java.util.Date;
import java.util.List;

/**
 * @author ：Yan Guang
 * @date ：Created in 2020/5/15 14:45
 * @description：
 */
public class CustomerDAOImp extends BaseDAO<Customer> implements CustomerDAO {


    @Override
    public void insert(Connection connection, Customer cust) {
        String sql = "insert into customers(name,email,birth)values(?,?,?)";
        update(connection, sql, cust.getName(), cust.getEmail(), cust.getBirth());
    }

    @Override
    public void deleteById(Connection connection, int id) {
        String sql = "delete from customers where id = ?";
        update(connection, sql, id);
    }

    @Override
    public void updateById(Connection connection, Customer cust) {
        String sql = "update customers set name=?,email =?,birth =? where id =?";
        update(connection, sql, cust.getName(), cust.getEmail(), cust.getBirth(), cust.getId());
    }

    @Override
    public Customer getCustomerById(Connection connection, int id) {
        String sql = "select id,name,email,birth from customers where id =?";
        Customer cust = getInstance(connection, sql, id);
        return cust;
    }

    @Override
    public List<Customer> getAll(Connection connection) {
        String sql = "select id,name,email,birth from customers";
        List<Customer> customerList= getForList(connection,  sql);
        return customerList;
    }

    @Override
    public Long getCount(Connection connection) {
        String sql = "select count(*) from customers";
        return getValue(connection, sql);
    }

    @Override
    public Date getMaxBirth(Connection connection) {
        String sql = "select max(birth) from customers";
        return getValue(connection, sql);
    }
}
